﻿using AutoMapper;
using HuaweiCloud.Dotnet.Samples.ESports.Device.Component;
using HuaweiCloud.Dotnet.Samples.ESports.Device.Dto;

namespace HuaweiCloud.Dotnet.Samples.ESports.Device.Dtos
{
    public class DefaultDtoProfile : Profile
    {
        public DefaultDtoProfile()
        {
            // 建议映射关系统一在这里配置，通过ReverseMap可以实现双向映射
            // 当然也可以通过AutoMapAttribute来使用单向映射
            CreateMap<CreateStepDto, Step>();
            CreateMap<Step, StepDto>();

            CreateMap<CreateSportDto, Sport>();
            CreateMap<Sport, SportDto>()
                .ForMember(
                    dest => dest.Duration, 
                    (op) => op.MapFrom(ori => (ori.EndTime - ori.StartTime).ToString(@"hh\:mm\:ss")))
                .ForMember(
                    dest => dest.Date,
                    (op) => op.MapFrom(ori => ori.StartTime.Date));
        }
    }
}
